﻿using System;
using System.Diagnostics;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using XiAnAirport.BUS;
using XiAnAirport.Models;

namespace XiAnAirport.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class NotificationController : ControllerBase

    {
        public IHttpContextAccessor _contextAccessor;
        private JwtSettings _jwtSettings;
        public NotificationController(IHttpContextAccessor contextAccessor, IOptions<JwtSettings> jwtSettingsAccesser)
        {
            _contextAccessor = contextAccessor;
            _jwtSettings = jwtSettingsAccesser.Value;
        }
        /// <summary>
        /// 新增通知公告
        /// </summary>
        /// <param name="noticeInfo">通知公告信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("AddNotice")]
        public ResponseMessage AddNotice(NoticeInfo noticeInfo)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                NoticeBUS noticeBUS = new NoticeBUS();
                message = noticeBUS.AddNotice(noticeInfo);
                if (message.errcode == 0)
                {
                    string username = HttpContext.Session.GetString("UserName").ToString();
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "新增通知公告" + noticeInfo.title;
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "新增通知公告失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "新增通知公告";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 修改通知公告
        /// </summary>
        /// <param name="noticeInfo">通知公告信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("UpdateNotice")]
        public ResponseMessage UpdateNotice(NoticeInfo noticeInfo)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                NoticeBUS noticeBUS = new NoticeBUS();
                message = noticeBUS.EditNotice(noticeInfo);
                if (message.errcode == 0)
                {
                    string username = HttpContext.Session.GetString("UserName").ToString();
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "修改通知公告" + noticeInfo.title;
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "修改通知公告失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "修改通知公告";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 删除通知公告
        /// </summary>
        /// <param name="noticeInfo">通知公告信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("DelNotice")]
        public ResponseMessage DelNotice(NoticeInfo noticeInfo)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                NoticeBUS noticeBUS = new NoticeBUS();
                message = noticeBUS.DelNotice(noticeInfo.id);
                if (message.errcode == 0)
                {
                    string username = HttpContext.Session.GetString("UserName").ToString();
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "删除通知公告" + noticeInfo.title;
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "删除通知公告失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "删除通知公告";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 查询通知公告
        /// </summary>
        /// <param name="noticeInfo">巡检计划信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryNoticeList")]
        public ResponseMessage QueryNoticeList(NoticeInfo noticeInfo)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                NoticeBUS noticeBUS = new NoticeBUS();
                message = noticeBUS.QueryNotice(noticeInfo);
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "查询通知公告失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询通知公告";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 查询通知公告
        /// </summary>
        /// <param name="noticeInfo">巡检计划信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("LoadNoticeInfo")]
        public ResponseMessage LoadNoticeInfo(NoticeInfo noticeInfo)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                NoticeBUS noticeBUS = new NoticeBUS();
                message = noticeBUS.LoadNotice(noticeInfo.id);
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "查询通知公告失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "载入通知公告";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 上架通知公告
        /// </summary>
        /// <param name="noticeInfo">巡检计划信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("ToppingNotice")]
        public ResponseMessage ToppingNotice(NoticeInfo noticeInfo)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                NoticeBUS noticeBUS = new NoticeBUS();
                message = noticeBUS.ToppingNotice(noticeInfo);
                if (message.errcode == 0)
                {
                    string username = HttpContext.Session.GetString("UserName").ToString();
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "上架通知公告" + noticeInfo.title;
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "上架通知公告失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "上架通知公告";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 批量删除通知公告
        /// </summary>
        /// <param name="noticeInfo">巡检计划信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("BatchDelNotice")]
        public ResponseMessage BatchDelNotice(string ids)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                NoticeBUS noticeBUS = new NoticeBUS();
                message = noticeBUS.BatchDelNotice(ids);
                if (message.errcode == 0)
                {
                    string username = HttpContext.Session.GetString("UserName").ToString();
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "批量删除通知公告" + ids;
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "批量删除通知公告失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "批量删除通知公告";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }
    }
}
